#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
const int MAXN=503;
int Edge[MAXN][MAXN];
ll dis[MAXN][MAXN];
bool _in[MAXN][MAXN];
int n,m;
struct node {
	int x,y;
	ll dis;
	bool operator < (const node& a) const {
		return dis>a.dis;
	}
};
void SPFA() {
	node now,pre;
	int len;
	memset(_in, 0, sizeof(_in));
	memset(dis, 0x3f, sizeof(dis));
	now.x=0; now.y=0; now.dis=0;
	queue<node> q;
	q.push(now);
	dis[now.x][now.y]=0;
	_in[now.x][now.y]=true;
	while (!q.empty()) {
		pre=q.front(); q.pop();
		_in[pre.x][pre.y]=false;
//		if (pre.x==m-1 && pre.y==n-1) break;
		int nex=Edge[pre.x][pre.y];
		int nex_xy=pre.x-nex;
		if (nex_xy>=0 && dis[pre.x][pre.y]+1 < dis[nex_xy][pre.y]) {
			dis[nex_xy][pre.y]=dis[pre.x][pre.y]+1;
			if (!_in[nex_xy][pre.y] && Edge[nex_xy][pre.y]!=0) {
				now.x=nex_xy; now.y=pre.y; now.dis=dis[now.x][now.y];
				q.push(now);
				_in[now.x][now.y]=true;
			}
		}
		nex_xy=pre.x+nex;
		if (nex_xy<m && dis[pre.x][pre.y]+1 < dis[nex_xy][pre.y]) {
			dis[nex_xy][pre.y]=dis[pre.x][pre.y]+1;
			if (!_in[nex_xy][pre.y] && Edge[nex_xy][pre.y]!=0) {
				now.x=nex_xy; now.y=pre.y; now.dis=dis[now.x][now.y];
				q.push(now);
				_in[now.x][now.y]=true;
			}
		}
		nex_xy=pre.y-nex;
		if (nex_xy>=0 && dis[pre.x][pre.y]+1 < dis[pre.x][nex_xy]) {
			dis[pre.x][nex_xy]=dis[pre.x][pre.y]+1;
			if (!_in[pre.x][nex_xy] && Edge[pre.x][nex_xy]!=0) {
				now.x=pre.x; now.y=nex_xy; now.dis=dis[now.x][now.y];
				q.push(now);
				_in[now.x][now.y]=true;
			}
		}
		nex_xy=pre.y+nex;
		if (nex_xy<n && dis[pre.x][pre.y]+1 < dis[pre.x][nex_xy]) {
			dis[pre.x][nex_xy]=dis[pre.x][pre.y]+1;
			if (!_in[pre.x][nex_xy] && Edge[pre.x][nex_xy]!=0) {
				now.x=pre.x; now.y=nex_xy; now.dis=dis[now.x][now.y];
				q.push(now);
				_in[now.x][now.y]=true;
			}
		}
	}
}
char ch[MAXN];
int main() {
	ll INF;
	memset(&INF, 0x3f, sizeof(INF));
//	freopen("in.txt", "r", stdin);
//	scanf("%d%d", &m, &n);
	while (~scanf("%d%d", &m, &n)) {
		for (int i=0; i<m; ++i) {
			scanf("%s", ch);
			for (int j=0; j<n; ++j) {
				Edge[i][j]=ch[j]-'0';
			}
		}
//		for (int i=0; i<m; ++i) {
//			for (int j=0; j<n; ++j) cout<<Edge[i][j]<<' ';
//			cout<<endl;
//		}
//		cout<<"-------"<<endl;
		SPFA();
//		for (int i=0; i<m; ++i) {
//			for (int j=0; j<n; ++j) printf("%d ", (dis[i][j]==INF? -1 : dis[i][j]));
//			cout<<endl;
//		}
		if (dis[m-1][n-1]==INF) {
			printf("IMPOSSIBLE\n");
		}
		else {
			printf("%I64d\n", dis[m-1][n-1]);
		}
	}
	return 0;
} 
